package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dto.JinyanDto;
import com.vf.admin.client.dto.LaheiDto;
import com.vf.admin.client.service.*;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppLaHeiController.java
 * @Description 拉黑接口
 * @createTime 2021年07月29日 10:10:00
 */
@Controller
@RequestMapping("/app/jinyan")
public class AppJinyanController extends ClientBaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppRoomJinyanService appRoomJinyanService;
    @Autowired
    AppRoomService appRoomService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppRoomAdminService appRoomAdminService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * @Description 房间禁言
     * @Param: [roomid 房间号, jyuserid 禁言的用户id]
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/29
     */
    @ResponseBody
    @RequestMapping("/roomjinyan")
    public Result roomjinyan(Integer roomid, Integer jyuserid) {
        try {
            if (roomid == null) {
                return this.errorJSON("房间号为空！");
            }
            if (jyuserid == null) {
                return this.errorJSON("用户id为空！");
            }
            AppRoom appRoom = appRoomService.getById(roomid);
            if (appRoom == null) {
                return this.errorJSON("房间不存在！");
            }
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            boolean flag = false;
            //操作用户是否为房主
            if (user.getId().equals(appRoom.getUserid())) {
                flag = true;
            }
            //操作用户是否为管理员
            AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(appRoom.getId(), user.getId());
            if (!StringUtils.isEmpty(roomAdmin)) {
                flag = true;
            }
            if (flag) {
                //获取禁言数据
                AppRoomJinyan appRoomJinyan = appRoomJinyanService.getByRoomidAndUserid(roomid, jyuserid);
                if (!org.springframework.util.StringUtils.isEmpty(appRoomJinyan)) {
                    return this.errorJSON("用户已经禁言！");
                }
                //获取禁言的用户信息
                AppUser lhappUser = appUserService.get(jyuserid);
                if (lhappUser == null) {
                    return this.errorJSON("禁言用户不存在！");
                }
                //添加拉黑信息
                AppRoomJinyan roomJinyan = new AppRoomJinyan();
                roomJinyan.setRoomid(roomid);//房间ID
                roomJinyan.setRoomname(appRoom.getRoomname());
                roomJinyan.setUserid(lhappUser.getId());//禁言的用户ID
                roomJinyan.setPic(lhappUser.getPic());//禁言的用户头像
                roomJinyan.setNick(lhappUser.getNick());//禁言的用户昵称
                roomJinyan.setPhone(lhappUser.getLoginname());//禁言的用户电话
                roomJinyan.setQianming(lhappUser.getMysign());//禁言用户的签名
                roomJinyan.setSex(lhappUser.getSex());//用户性别
                roomJinyan.setCreatetime(new Date());
                appRoomJinyanService.saveOrUpdate(roomJinyan);
                return this.sucessJSON("操作成功！");
            }else{
                return this.errorJSON("没有操作权限！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("房间禁言接口异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }


    /**
     * @Description 获取房间禁言列表
     * @Param: [roomid 房间号, pageon 页码数]
     * @return: com.vf.admin.common.entity.Result
     * @Author: xzx
     * @Date: 2021/07/29
     */
    @ResponseBody
    @RequestMapping("/roomlist")
    public Result roomlist(Integer roomid, Integer pageon) {
        try {

            if (roomid == null) {
                return this.errorJSON("房间号为空！");
            }

            if (pageon == null) {
                return this.errorJSON("页码为空！");
            }

            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));

            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);

            AppRoom appRoom = appRoomService.getById(roomid);
            if (appRoom == null) {
                return this.errorJSON("房间不存在！");
            }
            boolean flag = false;
            //操作用户是否为房主
            if (user.getId().equals(appRoom.getUserid())) {
                flag = true;
            }
            //操作用户是否为管理员
            AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(appRoom.getId(), user.getId());
            if (!StringUtils.isEmpty(roomAdmin)) {
                flag = true;
            }
            if (flag) {
                //分页数据
                SplitPageBean splitPageBean = new SplitPageBean(pageon, 20);
                //查询房间拉黑数据
                Map<String, Object> map = new HashMap<>(16);
                map.put("roomid", roomid);
                List<AppRoomJinyan> appRoomJianyanList = appRoomJinyanService.findList(map, splitPageBean);
                List<JinyanDto> jinyanDtoList = new ArrayList<>();
                for (AppRoomJinyan appRoomJinyan : appRoomJianyanList) {
                    JinyanDto dto = new JinyanDto();
                    dto.setUserid(appRoomJinyan.getUserid());//用户ID
                    dto.setPic(appRoomJinyan.getPic());//头像
                    dto.setNick(appRoomJinyan.getNick());//昵称
                    dto.setQianming(appRoomJinyan.getQianming());//签名
                    dto.setSex(appRoomJinyan.getSex());//性别
                    jinyanDtoList.add(dto);
                }
                return this.sucessPage(jinyanDtoList, splitPageBean.getNextPageNo());
            }else{
                return this.errorJSON("没有操作权限！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("获取房间禁言列表接口异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }

    /**
     * @Description 取消房间禁言
     * @Param: [roomid 房间号, jyuserid 禁言的用户id]
     * @return: com.vf.admin.common.entity.Result
     * @Author: xzx
     * @Date: 2021/07/29
     */
    @ResponseBody
    @RequestMapping("/canceljinyan")
    public Result canceljinyan(Integer roomid, Integer jyuserid) {
        try {
            if (roomid == null) {
                return this.errorJSON("房间号为空！");
            }
            if (jyuserid == null) {
                return this.errorJSON("用户id为空！");
            }
            AppRoom appRoom = appRoomService.getById(roomid);
            if (appRoom == null) {
                return this.errorJSON("房间不存在！");
            }
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //获取当前登录用户的user对象
            boolean flag = false;
            //操作用户是否为房主
            if (user.getId().equals(appRoom.getUserid())) {
                flag = true;
            }
            //操作用户是否为管理员
            AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(appRoom.getId(), user.getId());
            if (!StringUtils.isEmpty(roomAdmin)) {
                flag = true;
            }
            if (flag) {
                //获取禁言数据
                AppRoomJinyan jinyan = appRoomJinyanService.getByRoomidAndUserid(roomid, jyuserid);
                if (jinyan == null) {
                    return this.errorJSON("用户没有被拉黑！");
                }
                appRoomJinyanService.delete(jinyan.getId());
                return this.sucessJSON("操作成功！");
            }else{
                return this.errorJSON("没有操作权限！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("房间禁言接口异常" + e);
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }

}